package com.example.zhinongbao.activity;

import android.content.Intent;
import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

import com.example.zhinongbao.Fragment.FriendCircleFragment;
import com.example.zhinongbao.Fragment.HomeFragment;
import com.example.zhinongbao.Fragment.MessageFragment;
import com.example.zhinongbao.Fragment.ProfileFragment;
import com.example.zhinongbao.Fragment.SchoolFragment;
import com.example.zhinongbao.R;
import com.google.android.material.bottomnavigation.BottomNavigationView;

import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.DecelerateInterpolator;
import android.widget.LinearLayout;

public class MainActivity extends AppCompatActivity {
    private BottomNavigationView bottomNavigationView;
    private LinearLayout publishMenu;
    private boolean isPublishMenuShowing = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        bottomNavigationView = findViewById(R.id.bottom_navigation);
        publishMenu = findViewById(R.id.publish_menu_container);
        // 初始化时隐藏菜单
        publishMenu.setVisibility(View.GONE);
        publishMenu.setTranslationY(-publishMenu.getHeight());

        // 设置导航项选择监听
        bottomNavigationView.setOnNavigationItemSelectedListener(item -> {
            int itemId = item.getItemId();
            if (getIntent().hasExtra("open_fragment")) {
                switchToFragment(getIntent().getStringExtra("open_fragment"));
            }
            if (itemId == R.id.add) {
                togglePublishMenu();
                return false; // 不切换选中状态
            } else {
                // 如果菜单正在显示，先隐藏
                if (isPublishMenuShowing) {
                    hidePublishMenu();
                }

                Fragment fragment = null;
                if (itemId == R.id.home) {
                    fragment = new HomeFragment();
                } else if (itemId == R.id.school) {
                    fragment = new SchoolFragment();
                } else if (itemId == R.id.message) {
                    fragment = new MessageFragment();
                } else if (itemId == R.id.profile) {
                    fragment = new ProfileFragment();
                }

                if (fragment != null) {
                    getSupportFragmentManager().beginTransaction()
                            .replace(R.id.fl_content, fragment)
                            .commit();
                }
                return true;
            }
        });

        // 设置发布菜单项的点击事件
        setupPublishMenuClickListeners();

        // 默认加载首页
        bottomNavigationView.setSelectedItemId(R.id.home);
    }
    public void switchToFragment(String tag) {
        hidePublishMenu(); // 确保隐藏发布菜单

        Fragment fragment = null;
        if ("friend_circle".equals(tag)) {
            fragment = new FriendCircleFragment();
        }
        // 可以扩展其他Fragment标签

        if (fragment != null) {
            getSupportFragmentManager().beginTransaction()
                    .replace(R.id.fl_content, fragment)
                    .addToBackStack(tag) // 允许返回
                    .commit();
        }
    }
    private void togglePublishMenu() {
        if (isPublishMenuShowing) {
            hidePublishMenu();
        } else {
            showPublishMenu();
        }
    }

    private void showPublishMenu() {
        if (!isPublishMenuShowing) {
            isPublishMenuShowing = true;
            publishMenu.setVisibility(View.VISIBLE);

            // 从底部上滑动画
            publishMenu.animate()
                    .translationY(0)
                    .setDuration(300)
                    .setInterpolator(new DecelerateInterpolator())
                    .start();
        }
    }

    private void hidePublishMenu() {
        if (isPublishMenuShowing) {
            isPublishMenuShowing = false;

            // 下滑到底部隐藏动画
            publishMenu.animate()
                    .translationY(publishMenu.getHeight())
                    .setDuration(300)
                    .setInterpolator(new AccelerateInterpolator())
                    .withEndAction(() -> publishMenu.setVisibility(View.GONE))
                    .start();
        }
    }
    private void setupPublishMenuClickListeners() {
        findViewById(R.id.btn_publish_product).setOnClickListener(v -> {
            hidePublishMenu();
            startActivity(new Intent(this, PublishProductActivity.class));
        });

        // 其他菜单项的点击事件可以在这里添加
    }

    @Override
    public void onBackPressed() {
        if (isPublishMenuShowing) {
            hidePublishMenu();
        } else {
            super.onBackPressed();
        }
    }
    // 在MainActivity.java中添加：

}